package com.maomao.maomaochong_i.fenLeiPackage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

/**
 * Created by maomao on 2017/5/17.
 */

public class FenLeiPageNewTextView extends android.support.v7.widget.AppCompatTextView {
    public FenLeiPageNewTextView(Context context, AttributeSet attrs){
        super(context,attrs);
    }
    protected void onDraw(Canvas canvas){
        // 获取TextView的Drawable对象，返回的数组长度应该是4，对应左上右下
        Drawable[] drawables = getCompoundDrawables();

        if(drawables != null) {
            Drawable drawable = drawables[0];
            if (drawable != null) {
                // 当左边Drawable的不为空时，测量要绘制文本的宽度
                float textWidth = getPaint().measureText(getText().toString());
                int drawablePadding = getCompoundDrawablePadding();
                int drawableWidth = drawable.getIntrinsicWidth();

                // 计算总宽度（文本宽度 + drawablePadding + drawableWidth） 
                float bodyWidth = textWidth + drawablePadding + drawableWidth;

                // 移动画布开始绘制的X轴 
                canvas.translate((getWidth() - bodyWidth) /2, 0);
            } else if ((drawable = drawables[1]) != null){
                // 否则如果上边的Drawable不为空时，获取文本的高度
                Rect rect = new Rect();
                getPaint().getTextBounds(getText().toString(), 0,
                        getText().toString().length(), rect);
                float textHight = rect.height();
                int drawablePadding = getCompoundDrawablePadding();
                int drawableHeight = drawable.getIntrinsicHeight();

                // 计算总高度（文本高度 + drawablePadding + drawableHeight） 
                float bodyHeight = textHight + drawablePadding + drawableHeight;

                // 移动画布开始绘制的Y轴  
                canvas.translate(0, (getHeight() - bodyHeight) /2);
            }
        }
        super.onDraw(canvas);
    }
}

